package com.hitqz.robot.biz.flow.component.patrol;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hitqz.robot.api.business.dto.PatrolResultExcelDto;
import com.hitqz.robot.api.business.entity.PatrolResultEntity;
import com.hitqz.robot.api.business.model.dto.FlowContextDto;
import com.hitqz.robot.api.business.params.QueryPatrolSummaryParams;
import com.hitqz.robot.api.business.util.ExcelUtil;
import com.hitqz.robot.api.business.vo.PatrolResultSummaryVo;
import com.hitqz.robot.api.common.entity.PatrolObject;
import com.hitqz.robot.biz.core.FileProperties;
import com.hitqz.robot.biz.service.PatrolObjectService;
import com.hitqz.robot.biz.service.PatrolResultService;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.NodeComponent;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.stream.Collectors;

@Slf4j
@LiteflowComponent(value = "UploadResultFinal", name = "[巡检]上传结果-总")
@AllArgsConstructor
public class UploadResultFinal extends NodeComponent {

    private final PatrolResultService patrolResultService;

    private final ExecutorService customThreadPool;

    @Override
    public void process() throws Exception {
        FlowContextDto dto = this.getRequestData();
        Runnable runnableTask = () -> {
            Long ticketsId = dto.getTickets().getId();
            QueryPatrolSummaryParams query = new QueryPatrolSummaryParams();
            query.setTicketsId(ticketsId.intValue());
            List<PatrolResultEntity> list = patrolResultService.list(Wrappers.<PatrolResultEntity>lambdaQuery()
                    .eq(PatrolResultEntity::getTicketsId, ticketsId)
                    .ne(PatrolResultEntity::getUploadStatus, 1));
            if (list.isEmpty()){
                return;
            }
            for (PatrolResultEntity patrolResult : list) {
                patrolResultService.upload2Server(patrolResult);
                patrolResult.setUploadStatus(1);
                patrolResultService.saveOrUpdate(patrolResult);
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        };
        customThreadPool.submit(runnableTask);
    }

}